package com.boot.schedule;

import com.boot.init.StockRepository;
import com.boot.pojo.Product;
import com.boot.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StockSchedule {
    @Autowired
    private ProductService productService;
    @Autowired
    private StockRepository stockRepository;
    @Scheduled(fixedRate = 60000)
    public void syncStock() {
        List<Product> productList = productService.getProductList();
        if (productList != null && !productList.isEmpty()) {
            Map<String, String> stockMap = new HashMap<String, String>();
            for (Product product : productList) {
                stockMap.put("product:stock:" + product.getId(), String.valueOf(product.getStock()));
            }
            stockRepository.initStock(stockMap);
        }
    }
}
